<link href="<?php echo URL::base() ?>media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<link href="<?php echo URL::base() ?>media/vendor/css/layer.css" rel="stylesheet">
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.7.1.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.core.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.widget.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.position.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.autocomplete.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.7.1.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-1.10.4.custom.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-timepicker-addon.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-timepicker-zh-CN.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.datepicker-zh-TW.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/Validform_v5.3.2_ncr_min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/artdialog/jquery.artDialog.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/layer.js"></script>
<script type="text/javascript">

    var xmlHttp;
    function S_xmlhttprequest() {
        if(window.ActiveXObject) {
            xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
        } else if(window.XMLHttpRequest) {
            xmlHttp = new XMLHttpRequest();
        }
    }

    function url_jump(i,j,e)
    {
        //$(e).attr("disabled", true);

        var check_count = document.getElementById('check_count'+j).value;
        var ru_count = document.getElementById('ru_count'+j).value;
        var remark = document.getElementById('remark'+j).value;
        var buhege_count = document.getElementById('buhege_count'+j).value;
        var arrival_count = document.getElementById('arrival_count'+j).value;
        var store = document.getElementById('store' + j).value;

        if(check_count == 0){
            alert('质检量不能为空！');
            return false;
        }
        if(ru_count == ''){
            alert('请填写合格数量！');
            return false;
        }
        if(parseInt(buhege_count, 10) > 0 && remark == '') {
            alert('请填写不合格原因！');
            return false;
        }else if(parseInt(buhege_count, 10) == 0 && remark != ''){
            alert('没有不良品不需要填写不合格原因！');
            return false;
        }

        var count = document.getElementById('ru_count'+j).value;
        var count = Number(count);
        if(isNaN(count)){

            alert('数量格式非法！');
            document.getElementById('ru_count'+j).focus();
            return false;
        }

        var quality = document.getElementById('ru_count'+j).value;
        if(/^\d+$/.test(quality)){

        }else{
            alert('合格量输入有误！');
            document.getElementById('ru_count'+j).focus();
            return false;
        }

        if(parseInt(check_count,10) != parseInt(arrival_count,10)){
            alert('实际质检数量跟实际到货数量不相等，请检查！');
            return false;
        }

        if(parseInt(check_count,10) < parseInt(ru_count,10)){
            alert('合格数量有问题！');
            return false;
        }
        var uids = "";
       $("input[name^='unqualified_msg[]']:checked").each(function(i, el) {
            uids +=uids?','+$(this).val():$(this).val();
       });
        $("input[name=save_check]").attr('disabled','disabled');

        var ob = 'arrid='+i+'&arrvalid='+j+'&check_count='+check_count+'&ru_count='+ru_count+'&buhege_count='+buhege_count+'&remark='+remark+'&store='+store+'&uids='+uids;
        var url = "<?php echo url::base(true)?>quality/addcheck?"+ob;
        var ii = layer.load();
        $.ajax({
            type:'get',
            url:'<?php echo url::base(true);?>quality/addcheck?'+ob,
            dataType:'json',
            data:{},
            async:false,
            success:function(data)
            {
                layer.close(ii);
                var type = {
                    0 : '数据已保存过',
                    1 : '保存成功',
                    2 : '质检完成',
                    3 : '质检数量跟到货数据不对，请检查',
                    4 : '质检成功和入库成功',
                    5 : '质检失败,系统原因联系IT',
                    6 : '质检失败,待质检标准确认',
                    7 : '当前sku在当前仓库无储位，不能质检入库',
                    11 : '采购单入库数量大于采购单可入库数量，请记录收货明细单号，联系收货同事修改收货数量！'
                };
                alert(type[data]);
                if(data===11 || data===7){
                    $(e).parent().parent().css("background-color", 'red');
                    $("#save_check" + j).remove();
                }else{
                    location=location ;
                }
            },
            error:function(){
                alert("系统错误，请截图给IT");
            }
        });
    }

    function biaoqian(i,j)
    {

        var ob = '?arrid='+i+'&arrvalid='+j;
        var url = "<?php echo url::base(true)?>biaoqian/biaoqian"+ob;
        window.open(url);
    }

    function skubiaoqian(i,j)
    {

        var ob = '?arrid='+i+'&arrvalid='+j;
        var url = "<?php echo url::base(true)?>biaoqian/skubiaoqian"+ob;
        window.open(url);
    }

    function buhege(i,j)
    {

        var ob = '?arrid='+i+'&arrvalid='+j;
        var url = "<?php echo url::base(true)?>biaoqian/buhegebiaoqian"+ob;
        window.open(url);
    }

    function check_li(j)
    {
        var check_count = document.getElementById('check_count'+j).value;
        var arrival_count = document.getElementById('arrival_count'+j).value;
        var ru_count = document.getElementById('ru_count'+j).value;
        if(parseInt(check_count,10) != parseInt(arrival_count,10)){
            alert('实际质检数量跟实际到货数量不相等，请检查！');
        }

    }

    function checkForm(){
        var count = form1.receiveDetailId.value;
        var count = Number(count);
        if(isNaN(count)){

            alert('非法输入！');
            return false;
        }
    }

    function check_he(j)
    {

        var check_count = document.getElementById('check_count'+j).value;
        var ru_count = document.getElementById('ru_count'+j).value;
        var buhege_count = document.getElementById('buhege_count'+j).value;
        //var buhege = document.getElementById('buhege');
        var buhegeid = document.getElementById('buhegeid'+j);
        var buhegevalue = document.getElementById('buhege_count'+j);
        //var buhegebiao = document.getElementById('buhegebiao'+j);

        if(parseInt(check_count, 10) < parseInt(ru_count, 10)){
            alert('合格数量有问题！');
        }
        if(parseInt(check_count,10) > parseInt(ru_count,10)){

            //buhege.style.display = 'list-item';
            //buhegeid.style.display = 'list-item';
            //buhegebiao.style.display = 'list-item';
            var buhegel = parseInt(check_count,10)-parseInt(ru_count,10);
            buhegevalue.value = buhegel;

        }else{
            //buhege.style.display = 'none';
            //buhegeid.style.display = 'none';
            //buhegebiao.style.display = 'none';
            buhegevalue.value = 0;
        }
    }

    function byContent() {

        if(xmlHttp.readyState == 4 ){

            if(xmlHttp.status == 200) {
                var bytext =  xmlHttp.responseText;
                bytext = bytext.substr(0,1);
                if(bytext == 1){
                    alert('保存成功！');
                }else if(bytext == 0){
                    alert('数据已保存过！');
                }else if(bytext == 2){
                    alert('质检完成！');
                }else if(bytext == 3){
                    alert('质检数量跟到货数据不对，请检查！');
                }else if(bytext == 4){
                    alert('质检成功和入库成功！');
                }else if(bytext == 5){
                    alert('质检失败！');
                }
            }
        }
    }

</script>

    <form action="" method="get" name="form1" onsubmit="return checkForm();" class="form-horizontal">
        <div class="control-group">
            <label class="control-label">批次号：</label>
            <div class="controls">
                <input type='text' name="batch_code" value="<?php echo $batch_code;?>" id="batch_code" autocomplete="off">&nbsp;&nbsp;
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">收货明细单号：</label>
             <div class="controls">
                <input type='text' name="receiveDetailId" value="<?php echo $receiveDetailId;?>">&nbsp;&nbsp;
             </div>
        </div>
        <div class="control-group">
            <div class="controls">
            <input type='submit' name='sub' value='查询' class="btn btn-info">
            </div>
        </div>
    </form>


<?php if(isset($recieveDetails)){ ?>
    <!-- Table goes in the document BODY -->
    <table class="table table-bordered" >
        <tr>
<!--            <th><input type="checkbox" id="checkAllBtn">全选</th>-->
            <th>批次号</th>
            <th>供应商名称</th>
            <th>VMI预约单号</th>
            <th>收货明细单号</th>
            <th>sku</th>
            <th>中文名称</th>
            <th>所属仓库</th>
            <th>备注</th>
<!--            <th>质标确认</th>-->
            <th>属性</th>
            <th>到货量</th>
            <th>需质检量</th>
            <th>合格量</th>
            <th id="buhege">不合格量</th>
            <th>不合格原因</th>
            <th>操作</th>
        </tr>

        <?php foreach ($recieveDetails as $key => $recieveDetail) { ?>
            <tr <?php if($recieveDetail['checkId']) {?>style="background-color: yellowgreen;" <?php } ?>>
<!--                <td  class="<?php $recieveDetailId = $recieveDetail['id'];echo $recieveDetailId;?>">
                    <input type="checkbox" value="<?php echo $recieveDetail['recieve_invoice_id'];?>"
                           <?php if($recieveDetail['skuchecksign'] != 10 && !$recieveDetail['checkId']) {?>
                               class="tocheck" <?php
                           } else {
                               echo "disabled = 'disabled'";
                           }?>/><span><?php echo $key + 1;?></span>
                </td>-->
                <td><?php echo $recieveDetail['batch_code'];?></td>
                <td><?php echo $recieveDetail['suName'];?></td>
                <td><?php echo $recieveDetail['purchaseorder_id'];?></td>
                <td><?php echo $recieveDetail['id'];?></td>
                <td><?php echo "<b>" . $recieveDetail['sku'] . "</b>";?></td>
                <td><?php echo $recieveDetail['sku_name'];?></td>
                <td><?php echo $recieveDetail['storeName'];?></td>
                <td><?php echo $recieveDetail['puRemark'];?></td>
                <!--<td><?php echo $recieveDetail['skuchecksign'] == 10 ? "<span style='color: red'><b>待确认</b></span>" : ($recieveDetail['skuchecksign'] == 0 ? '无需确认' : '已确认');?></td>-->
                <td><label style="color: red"><?php echo $recieveDetail['logicStr'];?></label></td>
                <td><?php echo $recieveDetail['arrival_quantity'];?></td>
                <td><?php echo $recieveDetail['xu_check'];?></td>
                <?php if(!$recieveDetail['checkId']){ ?>
                    <td>
                        <input type='text' name="ru_count<?php echo $recieveDetailId;?>"
                               id="ru_count<?php echo $recieveDetailId;?>"
                               onBlur="check_he(<?php echo $recieveDetailId;?>);"
                               value="<?php echo $recieveDetail['xu_check']; ?>"
                               style='width:50px'>
                    </td>
                    <td id="buhegeid<?php echo $recieveDetailId;?>" style="height:124px;">
                        <input type='text' readOnly=true name="buhege_count<?php echo $recieveDetailId;?>"
                               id="buhege_count<?php echo $recieveDetailId;?>"
                               value="0" style='width:30px;margin-top:0px;'>
                    </td>
                    <td>
                        <input type='text' style="width:100px;" name="remark<?php echo $recieveDetailId;?>"
                               id="remark<?php echo $recieveDetailId;?>" >
                    </td>
                <?php } else {?>
                    <td><?php echo $recieveDetail['qualified_quantity'];?></td>
                    <td><?php echo $recieveDetail['unquality_count'];?></td>
                    <td><?php echo $recieveDetail['unquality_remark'];?></td>
                <?php } ?>
                <input type="hidden" name="store<?php echo $recieveDetailId;?>" id="store<?php echo $recieveDetailId;?>" value = "<?php echo isset($recieveDetail['store']) ? $recieveDetail['store'] : 0;?>">
                <input type='hidden' name="check_count<?php echo $recieveDetailId;?>" id="check_count<?php echo $recieveDetailId;?>" value="<?php echo $recieveDetail['xu_check']; ?>" style='width:50px'>
                <input type="hidden" name="arrival_count<?php echo $recieveDetailId;?>" id="arrival_count<?php echo $recieveDetailId;?>" value="<?php echo $recieveDetail['arrival_quantity']; ?>">

                <td width="105">
                    <?php if(!$recieveDetail['checkId'] && $recieveDetail['isSave'] && $recieveDetail['skuchecksign'] != 10){ ?>
                        <input type='button' name="save_check<?php echo $recieveDetailId;?>" class="btn btn-info"
                               id="save_check<?php echo $recieveDetailId;?>"
                               onclick="return url_jump(<?php echo $recieveDetail['recieve_invoice_id'];?>, <?php echo $recieveDetail['id'];?>, this)"
                               style="width:100px; margin-bottom:4px;" value='保存'>
                    <?php }?>

                    <input class="btn btn-success" type='button'
                           onclick="return biaoqian(<?php echo $recieveDetail['recieve_invoice_id'];?>, <?php echo $recieveDetailId;?>)"
                           style="width:100px; margin-bottom:4px;<?php if(empty($recieveDetail['qualified_quantity'])) echo 'display: none;'; ?>" value='打印良品标签'>

                    <input  class="btn btn-success"  type='button'
                            onclick="return skubiaoqian(<?php echo $recieveDetail['recieve_invoice_id'];?>, <?php echo $recieveDetailId;?>)"
                            style='width:100px' value='打印SKU标签'>

                    <?php if($recieveDetail['checkId'] && $recieveDetail['unquality_count'] > 0) {?>
                        <input class="btn btn-success"  type='button'
                           onclick="return buhege(<?php echo $recieveDetail['recieve_invoice_id'];?>,<?php echo $recieveDetailId;?>)"
                           style='width:100px;margin-left:0px; margin-top:4px;'
                           id="buhegebiao<?php echo $recieveDetailId;?>" value='不良品标签'>
                    <?php }?>
                    <input type="hidden" name="huoid<?php echo $recieveDetailId;?>" value="<?php echo $recieveDetail['recieve_invoice_id'];?>">
                </td>
            </tr>
            <tr>
            <td colspan="16">
            <?php 
            echo "分销直上不合格类型：&nbsp;&nbsp;&nbsp;&nbsp;";
            if(empty($recieveDetail['uids']) && empty($recieveDetail['checkId'])){
                foreach ($fxCheckMsg as $key => $value) {
                    echo '<input type="checkbox" name="unqualified_msg[]" value="'.$key.'">'.$value."&nbsp;&nbsp;&nbsp;&nbsp;";

                }
            }else{
                $uids = explode(',',$recieveDetail['uids']);
                foreach ($uids as $key => $value) {
                    echo $value."&nbsp;".$fxCheckMsg[$value]."&nbsp;&nbsp;&nbsp;&nbsp;";
                }
                
            }
             ?>    
            </td></tr>
            <?php
        }
        ?>
<!--        <tr><td colspan="5" style="text-align: left"><button class="btn btn-info" id="checkQuanlityMore">批量质检</button></td></tr>-->
    </table>

<?php } ?>

<script type="text/javascript">
    $(document).ready(function () {
        $("#checkAllBtn").click(function(){
            if(this.checked == true)
            {
                $(".tocheck").each(function(){
                    this.checked = true;
                });
            }
            else
            {
                $(".tocheck").each(function(){
                    this.checked = false;
                });
            }
        });

        $("#batch_code").autocomplete({
            autoFocus: true,
            minLength: 0,
            autoFill: true,
            source: '<?php echo URL::base(true)?>quality/getBatchCode',
            select: function (event, ui) {
                /**
                 * 从建议列表中选中之后触发的事件
                 */
                $("#batch_code").val(ui.item.name);
                $("#batch_code").attr("readonly", "readonly");
                return false;
            }
        });

        $("#checkQuanlityMore").click(function(){
            var quanlityMore = Array();
            var useAble = true;

            $(".tocheck").each(function(){
                if(this.checked == true){
                    var quanlityOne = {};
                    quanlityOne.rowNum = $(this).next("span").text();
                    quanlityOne.recieve_invoice_id = $(this).val();
                    var recieveDetailId = quanlityOne.recieve_detail_id = $(this).parent("td").attr('class');
                    quanlityOne.check_count = document.getElementById('check_count' + recieveDetailId).value;
                    quanlityOne.ru_count = document.getElementById('ru_count' + recieveDetailId).value;
                    quanlityOne.remark = document.getElementById('remark' + recieveDetailId).value;
                    quanlityOne.buhege_count = document.getElementById('buhege_count' + recieveDetailId).value;
                    quanlityOne.arrival_count = document.getElementById('arrival_count' + recieveDetailId).value;
                    quanlityOne.store = document.getElementById('store' + recieveDetailId).value;

                    if(quanlityOne.check_count == 0){
                        useAble = false;
                        alert('第' + quanlityOne.rowNum + '行的质检量不能为空！');
                        return false;
                    }
                    if(quanlityOne.ru_count == ''){
                        useAble = false;
                        alert('第' + quanlityOne.rowNum + '行的合格数量不能为空！');
                        return false;
                    }

                    var count = document.getElementById('ru_count' + recieveDetailId).value;
                    var count = Number(count);
                    if(isNaN(count)){
                        useAble = false;
                        alert('第' + quanlityOne.rowNum + '行的数量格式非法！');
                        document.getElementById('ru_count' + recieveDetailId).focus();
                        return false;
                    }

                    var quality = document.getElementById('ru_count' + recieveDetailId).value;
                    if(/^\d+$/.test(quality)){

                    }else{
                        useAble = false;
                        alert('第' + quanlityOne.rowNum + '行的合格量输入有误！');
                        document.getElementById('ru_count' + recieveDetailId).focus();
                        return false;
                    }

                    if(parseInt(quanlityOne.check_count, 10) != parseInt(quanlityOne.arrival_count, 10)){
                        useAble = false;
                        alert('第' + quanlityOne.rowNum + '行的实际质检数量跟实际到货数量不相等，请检查！');
                        return false;
                    }

                    if(parseInt(quanlityOne.check_count, 10) < parseInt(quanlityOne.ru_count, 10)){
                        useAble = false;
                        alert('第' + quanlityOne.rowNum + '行的合格数量有问题！');
                        return false;
                    }
                    //$("input[name=save_check]").attr('disabled','disabled');

                    quanlityMore.push(quanlityOne);
                }
            });

            if(useAble && quanlityMore.length == 0) {
                alert('请选择');
                return false;
            }

            if(useAble && quanlityMore.length > 0) {
                $.ajax({
                    type:'post',
                    url:'<?php echo url::base(true);?>quality/checkQuanlityMore',
                    dataType:'json',
                    data:{'checkQuanlityDetails' : quanlityMore},
                    async:false,
                    success:function(data)
                    {
                        var message = '';
                        var type = {
                            0 : '数据已保存过',
                            1 : '保存成功',
                            2 : '质检完成',
                            3 : '质检数量跟到货数据不对，请检查',
                            4 : '质检成功和入库成功',
                            5 : '质检失败,系统原因联系IT',
                            6 : '质检失败,待质检标准确认'
                        };
                        $.each(data, function(k, v) {
                            message += '第' + k + '行：' + type[v] + "\r\n";
                        });
                        alert(message);

                        window.location.href=window.location.href;
                        window.location.reload;
                    },
                    error:function(){
                        alert("error");
                    }
                });
            }
        });
    });
</script>
